using System;
using System.Data;
using System.Data.SqlClient;
using NNS.Framework;
using QLTB.BusinessObjects;

namespace QLTB.DataAccess
{
    /// <summary>
    /// Project: NNS System 
    /// Generated Class for Table : TBL_nhom.
    /// Date: 01/06/2010
    /// Author: Trần Hoàng Tâm
    /// </summary>
    public class TBL_nhomDA : Object
    {
        #region Exist

        public bool Exist
            (
            string tennhom
            )
        {
            bool _result = false;

            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader(CommandType.Text,
                                                                    "select * from TBL_nhom Where tennhom=@tennhom",
                                                                    new[] {new SqlParameter("@tennhom", tennhom)});
            if (myReader != null)
            {
                if (myReader.Read())
                {
                    _result = myReader.HasRows;
                    myReader.Close();
                }
            }

            SqlHelper.Helper.Close();

            return _result;
        }

        #endregion NewID

        #region Get

        public TBL_nhom Get(
            string tennhom)
        {
            TBL_nhom _n = null;
            SqlHelper.Helper.CommandType = CommandType.Text;
            SqlDataReader myReader = SqlHelper.Helper.ExecuteReader("select * from TBL_nhom Where tennhom=@tennhom",
                                                                    new[] {new SqlParameter("@tennhom", tennhom)});

            if (myReader != null)
            {
                while (myReader.Read())
                {
                    _n = Populate(myReader);
                }
                myReader.Close();
                myReader = null;
            }
            SqlHelper.Helper.Close();

            return _n;
        }

        #endregion

        public TBL_nhom Populate(
            IDataReader myReader)
        {
            TBL_nhom obj = null;

            if (myReader != null)
            {
                obj = new TBL_nhom();
                if (!myReader.IsDBNull(myReader.GetOrdinal("id")))
                {
                    obj.id = (int) myReader["id"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("tennhom")))
                {
                    obj.tennhom = (string) myReader["tennhom"];
                }
            }
            return obj;
        }

        #region Add

        public int Insert(
            int id,
            string tennhom)
        {
            SqlHelper.Helper.CommandType = CommandType.StoredProcedure;
            return SqlHelper.Helper.ExecuteNonQuery("TBL_nhom_Insert",
                                                    new[]
                                                        {
                                                            new SqlParameter("@id", id),
                                                            new SqlParameter("@tennhom", tennhom)
                                                        });
        }

        #endregion

        #region Update

        public int Update(
            int id,
            string tennhom)
        {
            SqlHelper.Helper.CommandType = CommandType.StoredProcedure;
            return SqlHelper.Helper.ExecuteNonQuery("TBL_nhom_Update",
                                                    new[]
                                                        {
                                                            new SqlParameter("@id", id),
                                                            new SqlParameter("@tennhom", tennhom)
                                                        });
        }

        #endregion

        #region Delete

        public int Delete(
            int id)
        {
            SqlHelper.Helper.CommandType = CommandType.StoredProcedure;
            return SqlHelper.Helper.ExecuteNonQuery("TBL_nhom_Delete", new[] {new SqlParameter("@Id", id)});
        }

        #endregion
    }
}